package com.bloveoa.zkkd.util;


import com.google.common.base.Strings;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;
import org.web3j.protocol.core.methods.response.EthTransaction;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.ClientTransactionManager;
import org.web3j.tx.gas.DefaultGasProvider;
import org.web3j.utils.Convert;
import token.Erc20Token;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.ExecutionException;

import static com.bloveoa.zkkd.util.Web3JClient.getWeb3j;

public class EthUtil {

    //加载合约
    public static Erc20Token loadErcToken(Web3j web3j, String contractAddress, Credentials credentials){
        return Erc20Token.load(contractAddress, web3j, credentials, DefaultGasProvider.GAS_PRICE.divide(new BigInteger("2")), new BigInteger("90000"));
    }
    public static Erc20Token loadErcToken(Web3j web3j, String contractAddress){
        return Erc20Token.load(contractAddress, web3j, new ClientTransactionManager(getWeb3j(),""), DefaultGasProvider.GAS_PRICE.divide(new BigInteger("2")), new BigInteger("90000"));
    }

    public static TransactionReceipt sendErc20Token(Credentials credentials, String toaddress, String count, String contractAddress) throws Exception {
        String number = ""+ Convert.toWei(count, Convert.Unit.ETHER);
        BigInteger bi = getBI(number);
        TransactionReceipt send = loadErcToken(getWeb3j(), contractAddress, credentials).transfer(toaddress,bi).send();
        return  send;
    }   public static TransactionReceipt sendErc20Token(String toaddress, String count) throws Exception {
        String number = ""+ Convert.toWei(count, Convert.Unit.ETHER);
        BigInteger bi = getBI(number);
        TransactionReceipt send = loadErcToken(getWeb3j(), "0x34b519150e0dc45a69051604e488da8dbbcd50ef", Credentials.create("5f9446f6cebd8192ae85b563c2f9fa2ce1f76e564529ec8a89dbb2bfb87065d1")).transfer(toaddress,bi).send();
        return  send;
    }

    public static BigInteger getBI(String num) {
        String zs = getZS(num);
        return new BigInteger(zs);
    }

    public static String getZS(String num) {
        if (Strings.isNullOrEmpty(num)) {
            return "0";
        }
        if (num.contains(".")) {
            return num.split("\\.")[0];
        } else {
            return num;
        }
    }

    /**
     * @param txhash
     * @return 0失败 1成功
     */
    public static String getStatus(String txhash) {
        String status = "0";
        try {
            EthGetTransactionReceipt ethGetTransactionReceipt = getWeb3j().ethGetTransactionReceipt(txhash).sendAsync().get();
            String status1 = ethGetTransactionReceipt.getTransactionReceipt().get().getStatus();
            if (!Strings.isNullOrEmpty(status1)) {
                status = status1.substring(2);
                System.out.println(status);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return status;
    }
    public static EthTransaction getEthTransaction(String transactionHash){
        Web3j web3 = getWeb3j();
        if(web3==null){
            return null;
        }
        EthTransaction tx = null;
        try {
            tx = web3.ethGetTransactionByHash(transactionHash).send();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tx;
    }

    public static void main(String[] args) {
        String data = "10000000000";
        BigDecimal bigDecimal = Convert.fromWei(data, Convert.Unit.ETHER);
        System.out.println(bigDecimal.toPlainString());

    }
}
